package top.blackr.folivora.common.security.auth;

import net.dreamlu.mica.core.utils.$;

import java.util.Optional;

public class SecurityUtils {

    private static final ThreadLocal<SecUser> secUserThreadLocal = new ThreadLocal<>();

    /**
     * 获取当前登录用户信息
     *
     * @return 当前登录用户信息
     */
    public static SecUser getUser() {
        return secUserThreadLocal.get();
    }

    public static void setUser(SecUser user) {
        secUserThreadLocal.set(user);
    }

    public static void clear() {
        secUserThreadLocal.remove();
    }

    /**
     * 是否有管理员权限
     *
     * @return true是；false否
     */
    public static boolean isAdmin() {
        SecUser secUser = getUser();
        if ($.isEmpty(secUser) || $.isEmpty(secUser.getRoles())) {
            return false;
        }
        Optional<String> roleAdmin = secUser.getRoles().stream().filter(roleCode -> $.equals("ROLE_ADMIN", roleCode)).findFirst();
        return roleAdmin.isPresent();
    }
}
